package algorithm;

import java.util.ArrayList;
import java.util.List;

public class leetcode_0046 {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list=new ArrayList<>();
        int len=nums.length;
        if(len==0) return list;
        boolean[]used=new boolean[len];
        List<Integer> path=new ArrayList<>();
        dfs(list,used,len,path,0,nums);
        return list;
    }
    public void dfs(List<List<Integer>> list,boolean[]used,int len,List<Integer> path,int index,int[] nums){
        if(index==len){
            list.add(new ArrayList<>(path));
            return;
        }
        for(int i=0;i<len;++i){
            if(!used[i]){
                path.add(nums[i]);
                used[i]=true;
                dfs(list,used,len,path,index+1,nums);
                path.remove(path.size()-1);
                used[i]=false;
            }
        }
    }
}
